
goog.provide('esn.client.SurveyClient');

goog.require('esn.client.BaseClient');
goog.require('esn.client.Response');
goog.require('esn.model.AnswerModel');
goog.require('esn.model.FriendSurveyModel');
goog.require('esn.model.StrainedFriendSurveyModel');
goog.require('goog.array');


/**
 * @constructor
 */
esn.client.SurveyClient = function(xhrManager) {
  goog.base(this, esn.client.SurveyClient.SERVICE_NAME, xhrManager);
};
goog.inherits(esn.client.SurveyClient, esn.client.BaseClient);

esn.client.SurveyClient.SERVICE_NAME = 'surveyservice';

esn.client.SurveyClient.Method = {
  DELETE_ANSWER: 'deleteAnswer',
  DELETE_FRIEND_SURVEY: 'deleteFriendSurvey',
  DELETE_STRAINED_FRIEND_SURVEY: 'deleteStrainedFriendSurvey',
  GET_FRIEND_SURVEYS: 'getFriendSurveys',
  GET_SECTION_ANSWERS: 'getSectionAnswers',
  GET_STRAINED_FRIEND_SURVEYS: 'getStrainedFriendSurveys',
  UPDATE_ANSWER: 'updateAnswer',
  UPDATE_FRIEND_SURVEY: 'updateFriendSurvey',
  UPDATE_FRIEND_SURVEYS: 'updateFriendSurveys',
  UPDATE_STRAINED_FRIEND_SURVEY: 'updateStrainedFriendSurvey'
};

esn.client.SurveyClient.prototype.getSectionAnswers = function(sectionId,
    callback) {
  var request = {'section_id': sectionId};
  this.send(
      esn.client.SurveyClient.Method.GET_SECTION_ANSWERS,
      request,
      goog.bind(this.getSectionAnswersResponse_, this, callback));
};

esn.client.SurveyClient.prototype.getSectionAnswersResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var answers = goog.array.map(responseJson['answers'], function(answerJson) {
      return new esn.model.AnswerModel(answerJson);
    });
    response.setData(answers);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.updateAnswer = function(answer, callback) {
  var request = {'answer': answer.toObject()};
  this.send(
      esn.client.SurveyClient.Method.UPDATE_ANSWER,
      request,
      goog.bind(this.updateAnswerResponse_, this, callback));
};

esn.client.SurveyClient.prototype.updateAnswerResponse_ = function(callback,
    responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var answer = new esn.model.AnswerModel(responseJson['answer']);
    response.setData(answer);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.deleteAnswer = function(id, callback) {
  var request = {'id': id};
  this.send(
      esn.client.SurveyClient.Method.DELETE_ANSWER,
      request,
      goog.bind(this.deleteAnswerResponse_, this, callback));
};

esn.client.SurveyClient.prototype.deleteAnswerResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.getFriendSurveys = function(callback) {
  var request = {};
  this.send(
      esn.client.SurveyClient.Method.GET_FRIEND_SURVEYS,
      request,
      goog.bind(this.getFriendSurveysResponse_, this, callback));
};

esn.client.SurveyClient.prototype.getFriendSurveysResponse_ = function(callback,
    responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var friendSurveys = goog.array.map(responseJson['friend_surveys'],
        function(friendSurveyJson) {
      return new esn.model.FriendSurveyModel(friendSurveyJson);
    });
    response.setData(friendSurveys);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.updateFriendSurvey = function(friendSurvey,
    callback) {
  var request = {'friend_survey': friendSurvey.toObject()};
  this.send(
      esn.client.SurveyClient.Method.UPDATE_FRIEND_SURVEY,
      request,
      goog.bind(this.updateFriendSurveyResponse_, this, callback));
};

esn.client.SurveyClient.prototype.updateFriendSurveyResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var friendSurvey =
        new esn.model.FriendSurveyModel(responseJson['friend_survey']);
    response.setData(friendSurvey);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.updateFriendSurveys = function(friendSurveys,
    opt_callback) {
  var friendSurveysJson = goog.array.map(friendSurveys,
      function(friendSurvey) {
    return friendSurvey.toObject();
  });
  var request = {'friend_surveys': friendSurveysJson};
  this.send(
      esn.client.SurveyClient.Method.UPDATE_FRIEND_SURVEYS,
      request,
      goog.bind(this.updateFriendSurveysResponse_, this, opt_callback || null));
};

esn.client.SurveyClient.prototype.updateFriendSurveysResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var friendSurveys = goog.array.map(responseJson['friend_surveys'],
        function(friendSurvey) {
      return new esn.model.FriendSurveyModel(friendSurvey);
    });
    response.setData(friendSurveys);
  }

  if (callback) {
    callback(response);
  }
};

esn.client.SurveyClient.prototype.deleteFriendSurvey = function(id, callback) {
  var request = {'id': id};
  this.send(
      esn.client.SurveyClient.Method.DELETE_FRIEND_SURVEY,
      request,
      goog.bind(this.deleteFriendSurveyResponse_, this, callback));
};

esn.client.SurveyClient.prototype.deleteFriendSurveyResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.getStrainedFriendSurveys = function(
    callback) {
  var request = {};
  this.send(
      esn.client.SurveyClient.Method.GET_STRAINED_FRIEND_SURVEYS,
      request,
      goog.bind(this.getStrainedFriendSurveysResponse_, this, callback));
};

esn.client.SurveyClient.prototype.getStrainedFriendSurveysResponse_ = function(
    callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var strainedFriendSurveys = goog.array.map(
        responseJson['strained_friend_surveys'],
        function(strainedFriendSurveyJson) {
      return new esn.model.StrainedFriendSurveyModel(strainedFriendSurveyJson);
    });
    response.setData(strainedFriendSurveys);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.updateStrainedFriendSurvey = function(
    strainedFriendSurvey, callback) {
  var request = {'strained_friend_survey': strainedFriendSurvey.toObject()};
  this.send(
      esn.client.SurveyClient.Method.UPDATE_STRAINED_FRIEND_SURVEY,
      request,
      goog.bind(this.updateStrainedFriendSurveyResponse_, this, callback));
};

esn.client.SurveyClient.prototype.updateStrainedFriendSurveyResponse_ =
    function(callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  } else {
    var strainFriendSurvey = new esn.model.StrainedFriendSurveyModel(
        responseJson['strained_friend_survey']);
    response.setData(strainFriendSurvey);
  }
  callback(response);
};

esn.client.SurveyClient.prototype.deleteStrainedFriendSurvey = function(id,
    callback) {
  var request = {'id': id};
  this.send(
      esn.client.SurveyClient.Method.DELETE_STRAINED_FRIEND_SURVEY,
      request,
      goog.bind(this.deleteStrainedFriendSurveyResponse_, this, callback));
};

esn.client.SurveyClient.prototype.deleteStrainedFriendSurveyResponse_ =
    function(callback, responseJson) {
  var response = new esn.client.Response();
  if ('error' in responseJson) {
    response.setError(responseJson['error']);
  }
  callback(response);
};
